/******************************************************************************

                                UTIL.H

  Title:  Include file needed by the full suite of programs
  Author:  Hanna Szoke
  Date:  3/3/93

******************************************************************************/

#include <stdlib.h>	/* declarations of malloc() et al. */
#include <stdio.h>
#include <math.h>
#include <ctype.h>	/* declarations of isalpha() etc. */
#include <string.h>	/* declarations of strcpy etc. */
#include <unistd.h>	/* Added 3/2/99, for Linux getopt() */
#include <sys/types.h>
#include <sys/file.h>
#include <sys/stat.h>

#define	PROGRAM		"Speden"
#define MAXSTRING       200     /* max length for reading character strings */
#define MAXLINES	200	/* max lines in an .inp file	*/
#define	UNSET		-1	/* initialization for reading input	*/

#define FALSE            0
#define TRUE             1

#define RTOD	180. / M_PI
#define DTOR	M_PI / 180.
#define PISQ	M_PI*M_PI
#define TWOPI	2*M_PI
#define RIGHT	90.	/* right angle */

#define AMP_EPSI   1.e-7	/* for eliminating round-off in amplitudes */
#define	EPSI_PHASE 1.e-3	/* for rounding phases very close to 0 or 180 */
#define ROUNDOFF   1.e-5  	/* to remove clutter in matching, reports */

#define	EPS	pow(2.0, -52.)
#define	BtoMB	pow(2.0, -20.)
#define	SQRT2	sqrt((real)2.) 

#define	COMMENT_CHAR	'#'	

#ifdef DOUBLE
	typedef	double	real;
        #define VERSION "SPEDEN 2.1 - Double Precision:"
#else
	typedef	float	real;
        #define VERSION "SPEDEN 2.1 - Single Precision:"
#endif

#define	BINSUF	".bin"
#define	HDFSUF	".hdf"

#define MAXNFILES	8	/* max # files for distance or variance calcs */

typedef struct {
  real re, im;
} COMPLEX;

typedef struct {
   real       x ;
   real       y ;
   real       z ;
} POINT ;

extern	FILE    *fp_log ;	/* pointer to log file */
extern	FILE    *fp_cost ;	/* pointer to cost file */
extern	char	log_filename[] ;	/* name of log file */
extern	char	caller[] ;	/* name of calling program (solve, back, etc.)*/
extern	char	command_line[] ;	/* command line to speden */
extern	char	message[] ;	/* general-purpose message buffer */
extern	char	*allinp ;	/* buffer for all input */
extern	char	nextline[] ;	/* ...for reading ASCII files	*/
extern	char	goodline[] ;	/* for identifying useful lines of input */
extern	char	terminp[] ;	/* used by prompt() to capture user input */
extern	char	id[] ;		/* first symbol on a line	*/
extern	int	Nlines ;	/* actual # of lines in .inp file */
extern	char	input_filename[] ;	/* parameter file name */
extern	char	sf_filename[] ;	/* generic structure factor file name */
extern	char	pwd_name[] ;	/* name of str. factor file, w/o path */
extern	char	out_filename[] ;	/* generic output file name */
extern	int	verbose ;	/* execute-line option -v */
extern	int	very_verbose ;	/* execute-line option -V */
extern	int	interactive ;	/* execute-line option -i */
extern	int	oneIt ;		/* execute-line option -o */
extern	int	graphics ;	/* execute-line option -g */
extern	int	quick ;		/* execute-line option -q */
extern	int	silent ;	/* execute-line option -s */
extern	char	costfile_option ;	/* save costs? (goes with -v)	*/
extern	float	t0, t1, t2, t3, t4, t5, t6 ;	/* for reading w. sscanf() */

extern	char	units[] ;	/* Angstrom, nm or micron */
extern	int	HDFIN ;
extern	int	HDFOUT ;

/***********************************************************
 * Commonly-used utility functions
 ***********************************************************/

extern	void	ballpark(char *) ;
extern	real    Cabs(COMPLEX *) ;              
extern	void	check_exist(char *) ;
extern	void	*e_malloc(unsigned int, char *) ;
extern	void	e_free(unsigned int, void *) ;
extern	void	extend_filename(char *, char *, char *) ;
extern	void	Fatal(char *) ;
extern	int	fetchTFStatus() ;
extern	int	filelength(char *) ;
extern	void    getAmpPhase(COMPLEX *, real, real *, real *) ;
extern	void	hello(char *) ;
extern	int	net_strlen(char *) ;		
extern	void	printTwice(char *) ;
extern	void	prompt(char *) ;
extern	void	readBasicInput() ;
extern	void	strip_path(char *, char *) ;
extern	void	strip_suffix(char *, char *, char *) ;
extern	char	*timestamp() ;	
extern	void	Warning(char *) ;

/***********************************************************
 * The SPEDEN methods
 ***********************************************************/
typedef void speden_method(int,char *[]);

speden_method
  apodfc_main,
  apodfo_main,
  back_main,
  bin2hdf_main,
  distance_main,
  hdf2bin_main,
  maketar_main,
  perturbhkl_main,
  regrid_main,
  solve_main,
  variance_main,
  weedfc_main,
  weedfo_main;
